package com.ymatou.iapi.trading.userquery.testcase;

import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.synctrade.parameter.mongo.UserIndex;
import com.ymatou.iapi.trading.userquery.parameter.HasBuyerEverPaidBean;
import com.ymatou.iapi.trading.userquery.service.HasBuyerEverPaidCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.mongo.UserIndexQueryWapper;
import com.ymttest.utils.EnvSetup;

/**
 * 获取相关订单详情Call FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_HasBuyerEverPaid {

	private static HasBuyerEverPaidBean hasBuyerEverPaidBean;
	private static HasBuyerEverPaidCall hasBuyerEverPaidCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询用户是否有支付过的订单");
	}

	@Before
	public void caseUp() {
		hasBuyerEverPaidBean = new HasBuyerEverPaidBean();
		hasBuyerEverPaidCall = new HasBuyerEverPaidCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(hasBuyerEverPaidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HasBuyerEverPaid_001() {
		Logger.start(true, "根据用户id查询用户是否有支付过的订单");
		try {

			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));

			List<UserIndex> orderList = new UserIndexQueryWapper()
					.findByUserId(userId);

			int actualCount = orderList.stream()
					.filter(UserIndex -> UserIndex.getPaidTime() != null)
					.collect(Collectors.toList()).size();

			hasBuyerEverPaidBean.setBuyerId(userId);

			String param = FormatUtil.beanToHttpString(hasBuyerEverPaidBean,
					true, false);

			hasBuyerEverPaidCall.setData(param);
			hasBuyerEverPaidCall.callService();

			Logger.verifyEquals(actualCount == 0 ? false : true,
					hasBuyerEverPaidCall.getData().getBoolean("yes"),
					"验证接口返回Data报文yes KEY值");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_HasBuyerEverPaid_002() {
		Logger.start(true, "userId 不存在");
		try {
			int userId = 132;
			hasBuyerEverPaidBean.setBuyerId(userId);
			String s = FormatUtil.beanToHttpString(hasBuyerEverPaidBean, true,
					false);
			hasBuyerEverPaidCall.setData(s);
			hasBuyerEverPaidCall.callService();

			Logger.verifyEquals(false, hasBuyerEverPaidCall.getData()
					.getBoolean("yes"), "验证接口返回Data报文yes KEY值");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_HasBuyerEverPaid_003() {
		Logger.start(true, "userId 为null");
		try {
			hasBuyerEverPaidBean.setBuyerId(null);
			String s = FormatUtil.beanToHttpString(hasBuyerEverPaidBean, true,
					false);
			hasBuyerEverPaidCall.setData(s);
			hasBuyerEverPaidCall.callService();

			Logger.verifyEquals("400", hasBuyerEverPaidCall.getCode(), "验证code");
			Logger.verifyEquals(100, hasBuyerEverPaidCall.getBCode(), "验证bcode");
			Logger.verifyEquals("buyerId:buyerId未提供",
					hasBuyerEverPaidCall.getMsg(), "验证msg");

			Logger.verifyEquals(false, hasBuyerEverPaidCall.getData()
					.getBoolean("yes"), "验证接口返回Data报文yes KEY值");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}
